/**
 * PC信息维护管理初始化
 */
var PcServerInfo = {
    id: "PcServerInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PcServerInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: ' 状态', field: 'status', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if(value=='0'){
                    return '禁用'
                }else{
                    return '使用'
                }
            }},
        {title: 'IP', field: 'ip', visible: true, align: 'center', valign: 'middle'},
        {title: '端口', field: 'port', visible: true, align: 'center', valign: 'middle'},
        // {title: 'cdkey', field: 'cdkey', visible: true, align: 'center', valign: 'middle'},
        {title: '服务器名称', field: 'serverName', visible: true, align: 'center', valign: 'middle'},
        {title: '机器码', field: 'machineCode', visible: true, align: 'center', valign: 'middle'},
        {title: '验证方式', field: 'validationType', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if(value == '0'){
                    return "离线"
                }else {
                    return "在线";
                }
            }
        },

        {title: '更新时间', field: 'updateTime', visible: true, align: 'center', valign: 'middle', width:150},
        {title: '更新人', field: 'updateUser', visible: true, align: 'center', valign: 'middle'},
       // {title: '创建时间', field: 'creatTime', visible: true, align: 'center', valign: 'middle',width:150},
       // {title: '创建人', field: 'createUser', visible: true, align: 'center', valign: 'middle'},
        {
            title: '操作', field: '', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                var a = '<button type="button" class="btn btn-primary btn-xs" onclick="PcServerInfo.downloadLic(\'' + row.id + ' \')">下载lic</button>';
                var b = "已禁用";
                if(row.status == '0'){
                    return b;
                }else {
                    return a;
                }

            }
        }
    ];
};

PcServerInfo.downloadLic = function (id) {

    var ajax = new $ax(Feng.ctxPath + "/pcServerInfo/downloadLic", function(data){
        console.info(Feng.ctxPath+"/"+data)
        $('<a id="lic" href="' + Feng.ctxPath + "/" + data + '"><button class="new-btn-login" id="subBtn" type="button"</a>')
            .appendTo('body');
        $("#subBtn").trigger("click");
        $("#lic").remove();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set("id",id);
    ajax.start();
    var url = Feng.ctxPath + "/pcServerInfo/downloadLic";

}



/**
 * 检查是否选中
 */
PcServerInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PcServerInfo.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加PC信息维护
 */
PcServerInfo.openAddPcServerInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加PC信息维护',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/pcServerInfo/pcServerInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看PC信息维护详情
 */
PcServerInfo.openPcServerInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: 'PC信息维护详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/pcServerInfo/pcServerInfo_update/' + PcServerInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 禁用PC信息维护
 */
PcServerInfo.disableSwitch = function (status) {
    if (this.check()) {
        if(this.seItem.status == status){
            var alert = status == '0' ? '状态为使用的才能禁用!' : '状态为禁用的才能启用!';
            Feng.error(alert);
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/pcServerInfo/disableSwitch", function (data) {
            Feng.success("操作成功!");
            PcServerInfo.table.refresh();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", this.seItem.id);
        ajax.set("status", status);
        ajax.start();
    }
};

/**
 * 查询PC信息维护列表
 */
PcServerInfo.search = function () {
    var queryData = {};
    queryData['serverName'] = $("#serverName").val();
    queryData['ip'] = $("#ip").val();
    queryData['status'] = $("#status").val();
    PcServerInfo.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = PcServerInfo.initColumn();
    var table = new BSTable(PcServerInfo.id, "/pcServerInfo/list", defaultColunms);
    table.setPaginationType("client");
    PcServerInfo.table = table.init();
});
